#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import config as c
from gitlab_map import site_map
import os
import re
import shutil
from glob import glob

root_path = c.local_www_path


def mycopyfile(srcfile, dstpath):
    fpath, fname = os.path.split(srcfile)              # 分离文件名和路径
    if not os.path.isfile(srcfile):
        print("%s not exist!" % (srcfile))
    else:
        fpath, fname = os.path.split(srcfile)          # 分离文件名和路径
        if not os.path.exists(dstpath):
            os.makedirs(dstpath)                       # 创建路径
        shutil.copy(srcfile, dstpath + fname)          # 复制文件
        print("copy %s -> %s" % (srcfile, dstpath + fname))


def alter(file, new_str, pattern):
    try:
        with open(file, "r", encoding="utf-8") as f1, open("%s.bak" % file, "w", encoding="utf-8") as f2:
            content = f1.read()
            match_ret = re.search(pattern, content)
            print(match_ret)
            flag = False
            if match_ret is None:
                print(file + ' ***** 异常: None,未匹配到 *****')
            else:
                text = re.sub(pattern, new_str, content)
                f2.write(text)
                flag = True
        if flag == True:
            os.remove(file)
            os.rename("%s.bak" % file, file)
            print(file + ' >>> 已完成')
        else:
            os.remove("%s.bak" % file)
    except Exception as e:
        print(file + ' ***** 异常 ***** >>> ' + str(e))

if __name__ == "__main__":
    for i in c.dirname_list:
        i = i.lower()
        dirname = site_map[i][35:][:-4].lower()
        project_path = root_path + dirname
        # 移动logo
        dstpath = 'G:/www/api.php'
        if os.path.exists(dstpath):
            shutil.copy(dstpath, project_path + '/public/api.php')

        # new_str = '''$flag = preg_match("/\/\.\.\/|\/\.\//", $_SERVER['REQUEST_URI']);'''
        # pattern = "\$flag.*_SERVER.*\;"
        # alter(project_path + '/public/api.php', new_str, pattern)

        # new_str = '''if(false '''
        # pattern = "if.*get_magic_quotes_gpc\(\)"
        # alter(project_path + '/common/common.func.php', new_str, pattern)
        # new_str = '''对要用于SQL的字符串进行处理，防止错误和注入 [php7.4不支持，未使用已废弃]'''
        # pattern = "\对要用于SQL的字符串进行处理，防止错误和注入"
        # alter(project_path + '/common/common.func.php', new_str, pattern)
        # new_str = '''$index[$start] . $index[$start + 1] . $index[$start + 2] . $index[$start + 3]'''
        # pattern = "\$index\{\$start\}.*3\}"
        # alter(project_path + '/common/libs/base.func.php', new_str, pattern)
        # new_str = '''$index[$start + 4] . $index[$start + 5] . $index[$start + 6]'''
        # pattern = "\$index\{\$start.*6\}"
        # alter(project_path + '/common/libs/base.func.php', new_str, pattern)
        # new_str = '''$index[$start + 7]'''
        # pattern = "\$index\{\$start\ \+\ 7\}"
        # alter(project_path + '/common/libs/base.func.php', new_str, pattern) 
        # new_str = '''if(false'''
        # pattern = "if.*get_magic_quotes_gpc\(\)"
        # alter(project_path + '/common/libs/alipay/alipay_core.function.php', new_str, pattern)
        # new_str = '''if(false)'''
        # pattern = ".*get_magic_quotes_gpc.*\)"
        # alter(project_path + '/site_admintAG8A4VZ/log.php', new_str, pattern)
        # new_str = ''''magic_quotes_gpc'=>(ini_set("magic_quotes_runtime", 0))? 'NO':'NO','''
        # pattern = "\'magic_quotes_gpc.*\'\,"
        # alter(project_path + '/site_admintAG8A4VZ/main.php', new_str, pattern)
        # new_str = ''''magic_quotes_runtime'=>(ini_set('magic_quotes_runtime', false))? 'NO':'NO','''
        # pattern = "\'magic_quotes_runtime.*\'\,"
        # alter(project_path + '/site_admintAG8A4VZ/main.php', new_str, pattern)
        # new_str = '''if(false'''
        # pattern = "if.*get_magic_quotes_gpc\(\)"
        # alter(project_path + '/common/common.simple.php', new_str, pattern)
        # new_str = '''if(false)'''
        # pattern = "if.*get_magic_quotes_gpc\(\)\)"
        # alter(project_path + '/site_admintAG8A4VZ/kindeditor/php/demo.php', new_str, pattern)
        # alter(project_path + '/kindeditor/php/demo.php', new_str, pattern)
        # new_str = '''$filename[0]'''
        # pattern = "\$filename\{0\}"
        # alter(project_path + '/site_admintAG8A4VZ/kindeditor/php/file_manager_json.php', new_str, pattern)
        # alter(project_path + '/kindeditor/php/file_manager_json.php', new_str, pattern)
        # new_str = '''$row['Value'][1]'''
        # pattern = "\$row\[\'Value\'\]\{1\}"
        # alter(project_path + '/common/libs/mysqli.class.php', new_str, pattern)
        # new_str = '''$row['Value'][1]'''      没执行
        # pattern = "_smarty_tpl->tpl_vars[$var]->value[$index]"
        # alter(project_path + '/common/libs/Smarty-3.1.39/sysplugins/smarty_internal_compile_for.php', new_str, pattern)
        # new_str = '''$url[0]'''
        # pattern = "\$url\{0\}"
        # alter(project_path + '/vendor\phpQuery\phpQuery\plugins\WebBrowser.php', new_str, pattern)
        # new_str = '''$param[1]'''
        # pattern = "\$param\{1\}"
        # alter(project_path + '/vendor\phpQuery\phpQuery\phpQueryObject.php', new_str, pattern)
        # new_str = '''$str[0]'''
        # pattern = "\$str\{0\}"
        # alter(project_path + '/common/phpExcel/PHPExcel/Shared/String.php', new_str, pattern)